﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 

public partial class management_Article_Category_Edit : System.Web.UI.Page
{
    private int id = Convert.ToInt32(HttpContext.Current.Request.QueryString["p"]);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDrpClass();
        }
    }

    //绑定顶级分类
    private void BindDrpClass()
    {
        Model.Article_Category Info = BLL.Article_Category.GetModel(id);
        txtTitle.Text = Info.Title;
        ckShows.Checked = Info.Shows;
        int total = 0;
        DataTable dt = BLL.Article_Category.Categorys_Get(0, int.MaxValue,0,out total);
        dropCategory.Items.Clear();
        dropCategory.Items.Add(new ListItem("一级分类", "0"));
        DataRow[] drs = dt.Select("FId= " + 0);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            //顶级分类显示形式
            classname = "╋" + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank = "├─";
            //递归子分类方法
            BindNode(sonparentid, dt, blank);
        }
        dropCategory.DataBind();
        dropCategory.SelectedValue = Info.FId.ToString();
    }

    //绑定子分类
    private void BindNode(int parentid, DataTable dt, string blank)
    {
        DataRow[] drs = dt.Select("FId= " + parentid);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            classname = blank + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank2 = blank + "─";
            BindNode(sonparentid, dt, blank2);
        }
    }

    //提交按钮事件
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        Model.Article_Category Info = new Model.Article_Category();
        Info.Title = txtTitle.Text;
        Info.FId = Convert.ToInt32(dropCategory.SelectedValue);
        Info.Shows = ckShows.Checked;
        Info.Id = id;
        Info.UpTime = DateTime.Now;
        if (Convert.ToInt32(dropCategory.SelectedValue) == id)
        {
            Page.RegisterStartupScript("back", "<script   languge='javascript'>alert('您的操作错误哦！');</script>");
            
        }
        else
        {
            BLL.Article_Category.Update(Info);
            Response.Redirect("Category_List.aspx");
        }
    }

    //返回按钮事件
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Category_List.aspx");
    }
}
